﻿// Author:					HungNM
// Created:					16-08-2012
// Last Modified:			16-08-2012
//

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;


namespace eProject.Web.UI
{

    public partial class UserManagementPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadParams();
            LoadSettings();
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateControls()
        {
            if (IsPostBack) return;
            BindUsers();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindUsers();
        }

        public string Gender(object gender)
        {
            if (gender != null)
            {
                return (bool)gender ? "Male" : "Female";
            }
            else
            {
                return "No";
            }
        }

        private void BindUsers()
        {
            string searchText = txtSearch.Text.Trim();
            bool isDeleted = chkShowDeleted.Checked;
            grvUsers.DataSource = Users.GetAllWithSearchText(searchText, isDeleted);
            grvUsers.DataBind();
        }

        protected void grvUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvUsers.PageIndex = e.NewPageIndex;
            grvUsers.DataBind();
            BindUsers();
        }

        protected void grvUsers_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Int64 Id = int.Parse(grvUsers.DataKeys[e.NewEditIndex].Value.ToString());
            Response.Redirect("EditUser.aspx?Id=" + Id);
        }

        protected void grvUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                Int64 Id = int.Parse(grvUsers.DataKeys[e.RowIndex].Value.ToString());
                bool isDeleted = !chkShowDeleted.Checked;
                Users.Delete(Id, isDeleted);
                BindUsers();
            }
            catch (System.Exception ex)
            {
                throw ex;	
            }
        }

        private void PopulateLabels()
        {
            Title = "User Page - eProject";
        }

        private void LoadSettings()
        {

        }

        private void LoadParams()
        {

        }

        #region OnInit

        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(this.Page_Load);
            this.grvUsers.RowDeleting += new GridViewDeleteEventHandler(grvUsers_RowDeleting);
            this.grvUsers.RowEditing += new GridViewEditEventHandler(grvUsers_RowEditing);
            this.grvUsers.PageIndexChanging += new GridViewPageEventHandler(grvUsers_PageIndexChanging);
            this.btnSearch.Click += new EventHandler(btnSearch_Click);
        }

        #endregion
    }
}